package org.example.Service;

import org.example.DTO.OrderDTO;
import org.example.mapper.OrderDetailsMapper;
import org.example.mapper.OrderMapper;
import org.example.pojo.Order;
import org.example.pojo.OrderDetails;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

@Service
public class OrderDTOServiceImpl implements OrderDTOService{

    @Autowired
    private OrderMapper orderMapper;

    @Autowired
    private OrderDetailsMapper orderDetailsMapper;

    @Override
    // 获取所有订单及其详情信息
    public List<OrderDTO> selectOrdersDTO() {
        List<OrderDTO> orderDTOList = new ArrayList<>();
        // 查询所有订单
        List<Order> orderList = orderMapper.selectOrders();

        for (Order order : orderList) {
            OrderDTO orderDTO = new OrderDTO();
            // 将Order实体属性复制到OrderDTO对象中
            BeanUtils.copyProperties(order, orderDTO);
            // 根据orderId查询订单详情
            List<OrderDetails> orderDetailsList = (List<OrderDetails>) orderDetailsMapper.selectOrderDetailsByID(order.getOrder_id());
            if (!orderDetailsList.isEmpty()) {
                OrderDetails orderDetails = orderDetailsList.get(0);
                // 将订单详情信息复制到OrderDTO对象中
                orderDTO.setDetail_id(orderDetails.getDetail_id());
                orderDTO.setName(orderDetails.getName());
                orderDTO.setPhone_number(orderDetails.getPhone_number());
                orderDTO.setAddress(orderDetails.getAddress());
                orderDTO.setProduct_id(orderDetails.getProduct_id());
                orderDTO.setQuantity(orderDetails.getQuantity());
            }
            orderDTOList.add(orderDTO);
        }

        return orderDTOList;
    }

    @Override
    // 根据orderId获取订单及其详情信息
    public OrderDTO selectOrdersDTOByID(Integer orderId) {
        OrderDTO orderDTO = new OrderDTO();
        // 根据orderId查询订单
        Order order = orderMapper.selectOrderByID(orderId);
        BeanUtils.copyProperties(order, orderDTO);
        // 根据orderId查询订单详情
        List<OrderDetails> orderDetailsList = (List<OrderDetails>) orderDetailsMapper.selectOrderDetailsByID(orderId);
        if (!orderDetailsList.isEmpty()) {
            OrderDetails orderDetails = orderDetailsList.get(0);
            // 将订单详情信息复制到OrderDTO对象中
            orderDTO.setDetail_id(orderDetails.getDetail_id());
            orderDTO.setName(orderDetails.getName());
            orderDTO.setPhone_number(orderDetails.getPhone_number());
            orderDTO.setAddress(orderDetails.getAddress());
            orderDTO.setProduct_id(orderDetails.getProduct_id());
            orderDTO.setQuantity(orderDetails.getQuantity());
        }

        return orderDTO;
    }

    @Override
    @Transactional
    // 创建订单及其详情
    public void insertOrderDTO(OrderDTO orderDTO) {
        Order order = new Order();
        BeanUtils.copyProperties(orderDTO, order);
        System.out.println(order.getOrder_id());
        // 插入订单
        orderMapper.insertOrder(order);

        OrderDetails orderDetails = new OrderDetails();
        BeanUtils.copyProperties(orderDTO, orderDetails);
        // 手动设置订单详情的order_id
        List<Order> list=orderMapper.selectOrders();
        order=list.get(list.size()-1);
        orderDetails.setOrder_id(order.getOrder_id());
        // 插入订单详情
        orderDetailsMapper.insertOrderDetails(orderDetails);
    }

    @Override
    // 更新订单及其详情
    public void updateOrderDTO(OrderDTO orderDTO) {
        Order order = new Order();
        BeanUtils.copyProperties(orderDTO, order);
        // 更新订单
        orderMapper.updateOrder(order);

        OrderDetails orderDetails = new OrderDetails();
        BeanUtils.copyProperties(orderDTO, orderDetails);
        // 更新订单详情
        orderDetailsMapper.updateOrderDetails(orderDetails);
    }

    @Override
    // 删除订单及其详情
    public void deleteOrderDTO(Integer orderId) {
        // 删除订单详情
        orderDetailsMapper.deleteOrderDetailsByOrderID(orderId);
        // 删除订单
        orderMapper.deleteOrder(orderId);
    }
}
